<?php

namespace app\sys\model;
use app\admin\model\Staff;
use \think\Model;

class TeamTime extends Model
{
    /**
     * 列表(list)
     * @param $search
     * @param $page
     * @param $limit
     * @return array
     */
    public static function ModelList($search,$page,$limit):array
    {
        $userSql = TeamTime::field("*");
        if($search){
            $userSql = $userSql->where('name',"like", "%".$search."%");
        }
        $count = $userSql->count();
        $list = $userSql->page($page,$limit)->order('id', 'asc')->select();
        return [$count,$list];
    }

    /**
     * 添加(add)
     * @param $teamId
     * @param array $month
     * @param array $nextMonth
     * @param array $otherDay
     * @param int $week
     * @return string
     * @throws \Exception
     */
    public static function ModelAdd($teamId,$month = [],$nextMonth = [],$otherDay = [],$week = []):string
    {
        TeamTime::where("team_id",$teamId)->delete();

        foreach ($month as $k => $v){
            $v = strtotime($v);
            TeamTime::create(["team_id"=>$teamId,"day"=>$v,"type"=>"new"]);
        }

        foreach ($nextMonth as $k => $v){
            $v = strtotime($v);
            TeamTime::create(["team_id"=>$teamId,"day"=>$v,"type"=>"next"]);
        }

        foreach ($otherDay as $k => $v){
            $v = strtotime($v);
            TeamTime::create(["team_id"=>$teamId,"day"=>$v,"type"=>"other"]);
        }
        foreach ($week as $k => $v) {
            if (in_array($v, [6, 7])) {
                TeamTime::create(["team_id" => $teamId, "day" => $v, "type" => "week"]);
            }
        }

        return "";
    }

    /**
     * 修改（update）
     * @param $id
     * @param $data
     * @return string
     */
    public static function ModelUp($id,$data):string
    {
        $userInfo = TeamTime::where("id",$id)->find();
        if(!$userInfo){
            return "数据不存在~";
        }
        $resUp = TeamTime::where("id",$id)->data($data)->update();
        if(!$resUp){
            return "修改失败~";
        }
        return "";
    }

    /**
     * 删除（delete）
     * @param array $id
     * @return string
     */
    public static function ModelDel($id=[]):string
    {
        $userInfo = TeamTime::where("id","in",$id)->select();
        if(empty($userInfo)){
            return "数据不存在~";
        }
        TeamTime::where("id","in",$id)->delete();

        return "";
    }

    /**
     * 详情（detail）
     * @param $id
     * @return Model
     */
    public static function ModelDet($id):Model
    {
        return TeamTime::where("id",$id)->find();
    }


}